//
//  PayMoneyController.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/5.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "PayMoneyController.h"
#import "KeepCallBottomView.h"
#import "GetMoneyTextFieldCell.h"
#import "GetMoneyInfoModel.h"
#import "KeepCallsPayCell.h"
#import "KeepCallSectionHeader.h"
#import "AboutusController.h"
#import "DateSelectCell.h"
#import <WXApi.h>
#import <AlipaySDK/AlipaySDK.h>

#import <LEEAlert/LEEAlert.h>
#import "SelectedListView.h"

#import "PaySuccessJumpVC.h"
#import "PayFaildJumpVC.h"
@interface PayMoneyController ()
@property (nonatomic, strong) KeepCallBottomView *footerView;

/** <#note#> */
@property (nonatomic, strong) GetMoneyInfoModel *infoModel;

@property (nonatomic, strong) NSMutableArray<NSDictionary *> *dateArray;

/** <#note#> */
@property (nonatomic, strong) NSString *out_trade_no;

/** <#note#> */
@property (nonatomic, weak) UILabel *priceLabel;


/** <#note#> */
@property (nonatomic, strong) NSString *thisYeay;
/** <#note#> */
@property (nonatomic, strong) NSString *payTag;
@end

@implementation PayMoneyController


- (NSString *)thisYeay{
    if (!_thisYeay) {
        
        NSCalendar *gregorian = [PayMoneyController xmg_calendar];
        
        // 获取当前日期
        NSDate* dt = [NSDate date];
        // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
        unsigned unitFlags = NSCalendarUnitYear |
        NSCalendarUnitMonth |  NSCalendarUnitDay |
        NSCalendarUnitHour |  NSCalendarUnitMinute |
        NSCalendarUnitSecond | NSCalendarUnitWeekday;
        // 获取不同时间字段的信息
        NSDateComponents* comp = [gregorian components: unitFlags
                                              fromDate:dt];
        
        
        _thisYeay = [NSString stringWithFormat:@"%ld",(long)comp.year];
    }
    
    return [NSString stringWithFormat:@"%@ 年",_thisYeay];
    
}


+ (NSCalendar *)xmg_calendar
{
    if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
        
        return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    }
    return [NSCalendar currentCalendar];
}


- (NSMutableArray<NSDictionary *> *)dateArray{
    
    if (_dateArray == nil) {
        
        int this = self.thisYeay.intValue;
        
        _dateArray = [[NSMutableArray alloc]init];
        
        for (int i = this; i < self.thisYeay.integerValue + 2; i ++) {
            
            NSMutableDictionary *mutDictionary = [[NSMutableDictionary alloc]init];
            mutDictionary[@"name"] = [NSString stringWithFormat:@"%.2d年",i];
            NSMutableArray<NSMutableDictionary *> *tempArray = [[NSMutableArray alloc]init];
            
            for (int j = 1; j < 13; j ++) {
                NSMutableDictionary *subDictionary = [[NSMutableDictionary alloc]init];
                subDictionary[@"name"] = [NSString stringWithFormat:@"%.2d月",j];
                
                NSMutableArray *subArray = [[NSMutableArray alloc]init];
                
                for (int k = 1; k < 32 ; k ++ ) {
                    [subArray addObject:[NSString stringWithFormat:@"%.2d日",k]];
                }
                subDictionary[@"sub"] = subArray;
                
                [tempArray addObject:subDictionary];
                
            }
            mutDictionary[@"sub"] = tempArray;
            
            [_dateArray addObject:mutDictionary];
            
        }
        
    }
    
    
    return _dateArray;
    
}

- (GetMoneyInfoModel *)infoModel{
    
    if (!_infoModel) {
        
        _infoModel = [[GetMoneyInfoModel alloc]init];
        
        _infoModel.alipay = @"1";
        
        _infoModel.wechatPay = @"0";
        
    }
    
    
    
    return _infoModel;
}



- (KeepCallBottomView *)footerView{
    
    if (!_footerView) {
        
        KeepCallBottomView *temp = (KeepCallBottomView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([KeepCallBottomView class]) owner:nil options:nil].firstObject;
        
        
        
        [temp.topBtn setTitle:@"立即支付" forState:UIControlStateNormal];
        [temp.topBtn setTitle:@"立即支付" forState:UIControlStateHighlighted];
        
        [temp.topBtn addTarget:self action:@selector(payMoney) forControlEvents:UIControlEventTouchUpInside];
        
//        temp.backgroundColor = [UIColor redColor];
        CGFloat viewH = IPhoneX ? 88 : 64;
        temp.frame = CGRectMake(0, 0, KScreenW, KScreenH - 396 - viewH);
        
        _footerView = temp;
        
        [self.view addSubview:temp];
        
    }
    
    return _footerView;
}


- (void)payMoney{
    
    [self.view endEditing:YES];
    

    
   
    [self calculateRmb];
    
    if (self.infoModel.huNumber == nil) {
        
        [self showInformation:@"请填写户号"];
        
        return;
    }
    

    
//    if (![[NetworkTools shareNetworkTools]isHuHao:self.infoModel.huNumber]) {
//        
//        [self showInformation:@"户号格式错误"];
//
//        
//        return;
//    }

    
    if (self.infoModel.money == nil) {
        
        [self showInformation:@"请输入提现金额"];
        
        return;
    }
    
    
    
    
    //判断是否是纯数字
    
    if( [self isPureInt:self.infoModel.money] || [self isPureFloat:self.infoModel.money])
        
    {
        
        
        if (!(self.infoModel.money.floatValue > 0)) {
            
            [self showInformation:@"输入金额非法"];
            
            return;
        }
        
        
        
        if (self.infoModel.billNumber == nil) {
            
            [self showInformation:@"请填写账单号"];
            
            return;
        }
        
        
//        if (![[NetworkTools shareNetworkTools]isBillNumber:self.infoModel.billNumber]) {
//
//            [self showInformation:@"账单号格式错误"];
//
//
//            return;
//        }
        
        
        
        
//        stoptime
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"payment"] = self.infoModel.money;
        parameters[@"uid"] = myViewModel.user.uid;
        parameters[@"account_number"] = self.infoModel.huNumber;
        parameters[@"bill_num"] = self.infoModel.billNumber;

        if ([self.type isEqualToString:@"电费"]) {
            
            parameters[@"type"] = @"1";
            
            
            if (self.infoModel.dateStr == nil) {
                
                [self showInformation:@"请选择截止日期"];

                return;
                
            }else{
                
                
                if ([self panDuanDate]) {
                    return;
                }
            }
            
            NSString *date = [self.infoModel.dateStr substringToIndex:self.infoModel.dateStr.length - 1];
            
            
            date = [date stringByReplacingOccurrencesOfString:@"月" withString:@"-"];

            date = [date stringByReplacingOccurrencesOfString:@"年" withString:@"-"];

//            NSLog(@"%@",date);
            
            parameters[@"stoptime"] = date;

            
        } else {
            
            parameters[@"type"] = @"2";
             parameters[@"supplier_id"] = self.infoModel.supplier_id;
            parameters[@"supplier_name"] = self.infoModel.dateStr;

            
        }
        
        [SVProgressHUD showWithStatus:@"订单确认中..."];
        
        MJWeakSelf;
        [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Order/virtualOrderCreate" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
            
            
            if (!error) {
                
                
                [weakSelf lastPayWitPayment:responseObject[@"data"][@"pay_rmb"] outTradeNo:responseObject[@"data"][@"out_trade_no"]];
                
                weakSelf.out_trade_no = responseObject[@"data"][@"out_trade_no"];
                
                if ([weakSelf.infoModel.dateStr isEqualToString:@"PLDT"]) {
                    
                    myViewModel.user.huHao = weakSelf.infoModel.huNumber;
                    
                    myViewModel.user.billNumber = weakSelf.infoModel.billNumber;
                    
                    [myViewModel.user saveUserInfo];
                    
                }

            } else {
                
                
                [SVProgressHUD showInfoWithStatus:error.domain];
            }
        }];
        
       
    }else{
        
        
        [self showInformation:@"输入金额非法"];
        
        
        
        
        
    }
    
  
    
    
}


- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
    
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    
    
    
    
}

- (void)calculateRmb{
    
    
    if (self.infoModel.money == nil) {
        
        
        return;
    }
    
    
    //判断是否是纯数字
    
    if( [self isPureInt:self.infoModel.money] || [self isPureFloat:self.infoModel.money])
        
    {
        
        
        if (!(self.infoModel.money.floatValue > 0)) {
            
            
            return;
        }
        
        
        
        
        self.priceLabel.text = [NSString stringWithFormat:@"%.2f",self.infoModel.money.floatValue * myViewModel.rate.floatValue];
        
        
    }
    
    
}
    
    
    
- (void)showInformation:(NSString *)str{
        
    UIAlertController *contr = [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    
//    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//
//    }];
//
//
//    [contr addAction:action2];
    [contr addAction:action1];
    
    [self presentViewController:contr animated:YES completion:nil];
    
        
        
        
}
    
    
    
    
- (BOOL)isPureInt:(NSString*)string{
        
        NSScanner* scan = [NSScanner scannerWithString:string];
        
        int val;
        
        return [scan scanInt:&val] && [scan isAtEnd];
        
    }
    
    
    //判断是否为浮点形：
    
- (BOOL)isPureFloat:(NSString*)string{
        
        NSScanner* scan = [NSScanner scannerWithString:string];
        
        float val;
        
        return [scan scanFloat:&val] && [scan isAtEnd];
        
    }




- (void)lastPayWitPayment:(NSString *)payment outTradeNo:(NSString *)trade{
    //    ?out_trade_no=&pay_type=2&payment=0.01
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"payment"] = payment;
    parameters[@"out_trade_no"] = trade;
    
    if ([self.infoModel.alipay isEqualToString:@"1"]) {
        
        parameters[@"pay_type"] = @"2";
        
        [self AlipayPayWith:parameters];
        
    } else {
        
        parameters[@"pay_type"] = @"1";
        
        [self wachatPay:parameters];
        
    }
    

    
}



- (void)AlipayPayWith:(NSMutableDictionary *)parameters{
    
    
    
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Pay/pay_order" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
            
            [SVProgressHUD dismiss];
            
            [[AlipaySDK defaultService] payOrder:responseObject[@"data"] fromScheme:@"com.beisheng.feifubao" callback:^(NSDictionary *resultDic) {
                
                
//                NSLog(@"reslut = %@",resultDic);
                
            }];
            
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
        
    }];
    
    
    
}

- (void)wachatPay:(NSMutableDictionary *)parameters{
    
    
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"Pay/pay_order" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
            
            [SVProgressHUD dismiss];
            
//            MYJSONSTR;
            
            [self WXPayWithDic:responseObject[@"data"]];
            
            
        } else {
            
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
        
    }];
    
    
    
    
    
    
    
}



- (void)WXPayWithDic:(NSDictionary *)dic{
    
    //需要创建这个支付对象
    PayReq *req   = [[PayReq alloc] init];
    //由用户微信号和AppID组成的唯一标识，用于校验微信用户
    req.openID = dic[@"appid"];
    
    // 商家id，在注册的时候给的
    req.partnerId = dic[@"partnerid"];
    
    // 预支付订单这个是后台跟微信服务器交互后，微信服务器传给你们服务器的，你们服务器再传给你
    req.prepayId  = dic[@"prepayid"];
    
    // 根据财付通文档填写的数据和签名
    //这个比较特殊，是固定的，只能是即req.package = Sign=WXPay
    req.package   = dic[@"pkg"];
    
    // 随机编码，为了防止重复的，在后台生成
    req.nonceStr  = dic[@"noncestr"];
    
    // 这个是时间戳，也是在后台生成的，为了验证支付的
    NSString * stamp = dic[@"timestamp"];
    
    req.timeStamp = stamp.intValue;
    
    // 这个签名也是后台做的
    req.sign = dic[@"sign"];
    
    //发送请求到微信，等待微信返回onResp
    [WXApi sendReq:req];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self.type isEqualToString:@"电费"]) {
        
        self.navigationItem.title = @"缴纳电费";
        
    } else {
        
        self.navigationItem.title = @"缴纳网费";
        
        self.infoModel.dateStr = myViewModel.parameterModel.networkSupplier.lastObject.supplier_name;
        
        self.infoModel.supplier_id = myViewModel.parameterModel.networkSupplier.lastObject.supplier_id;
        
        
        
        if ([self.infoModel.dateStr isEqualToString:@"PLDT"]) {
            
            if (myViewModel.user.huHao.length > 0) {
                
                if (self.infoModel.huNumber == nil) {
                    self.infoModel.huNumber = myViewModel.user.huHao;
                }
                
            }
            
            
            if (myViewModel.user.billNumber.length > 0) {
                
                if (self.infoModel.billNumber == nil) {
                    self.infoModel.billNumber = myViewModel.user.billNumber;
                }
                
            }
            
        }

    }
    
    
    [self setNav];
    
    self.payTag = @"0";
    
    [self.tableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:[BaseTableViewCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([KeepCallsPayCell class]) bundle:nil] forCellReuseIdentifier:[KeepCallsPayCell identifierStr]];

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([KeepCallSectionHeader class]) bundle:nil] forHeaderFooterViewReuseIdentifier:[KeepCallSectionHeader identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GetMoneyTextFieldCell class]) bundle:nil] forCellReuseIdentifier:[GetMoneyTextFieldCell identifierStr]];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DateSelectCell class]) bundle:nil] forCellReuseIdentifier:[DateSelectCell identifierStr]];

    self.tableView.tableFooterView = self.footerView;
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"wenhao"] style:UIBarButtonItemStylePlain target:self action:@selector(setNav)];
    
    

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WXApiDidSuccessNotification) name:WXApiDidSuccessNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(WXApiDidFailNotification) name:WXApiDidFailNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AlipayDidSuccessNotification) name:AlipayDidSuccessNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(AlipayDidFailNotification) name:AlipayDidFailNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(calculateRmb) name:@"StartCalculateRmb" object:nil];
    

    
}



- (void)setNav{
    
    UIView *navRit = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 42, 36)];
    
    navRit.backgroundColor = [UIColor clearColor];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 0, 25, 36)];
    
    imageView.backgroundColor = [UIColor clearColor];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    
    imageView.image = [UIImage imageNamed:@"wenhao"];
    
   
    //    conutLabel.hidden = YES;
    
    UIButton *jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    jumpBtn.frame = CGRectMake(0, 0, 42, 36);
    
    jumpBtn.backgroundColor = [UIColor clearColor];
    
    [jumpBtn addTarget:self action:@selector(wenHaoBtnDidTap) forControlEvents:UIControlEventTouchUpInside];
    
    [navRit addSubview:imageView];
    
  
    
    [navRit addSubview:jumpBtn];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navRit];
    
    
}

- (void)wenHaoBtnDidTap{
    
    [self.view endEditing:YES];
    
    [self calculateRmb];
    
    
    
    if ([self.type isEqualToString:@"电费"]) {
        
        AboutusController *tempVC = [[AboutusController alloc]init];
        
        tempVC.urlString = self.urlStr;
        
        tempVC.type = @"dian";
        
        [self.navigationController pushViewController:tempVC animated:YES];
        
        
    } else {
        
        AboutusController *tempVC = [[AboutusController alloc]init];
        
        tempVC.urlString = myViewModel.internet_information;
        
        tempVC.type = @"dian";
        
        [self.navigationController pushViewController:tempVC animated:YES];
    }
    
    
}


- (void)WXApiDidSuccessNotification{
    
    if ([self.payTag isEqualToString:@"0"]) {
        self.payTag = @"1";
        
        PaySuccessJumpVC *jump = [[PaySuccessJumpVC alloc]init];
        
        jump.order_id = self.out_trade_no;
        
        [self.navigationController pushViewController:jump animated:YES];
    }
    
    


    
//    [SVProgressHUD showSuccessWithStatus:@"支付成功!"];
//
//    [self.navigationController performSelector:@selector(popViewControllerAnimated:) withObject:@1 afterDelay:2.0];
    
}

- (void)WXApiDidFailNotification{
    if ([self.payTag isEqualToString:@"0"]) {
        
        self.payTag = @"1";
    PayFaildJumpVC *jump = [[PayFaildJumpVC alloc]init];
    
      jump.order_id = self.out_trade_no;
    [self.navigationController pushViewController:jump animated:YES];
    }
}

- (void)AlipayDidSuccessNotification{
    
    if ([self.payTag isEqualToString:@"0"]) {
        
        self.payTag = @"1";
        
        PaySuccessJumpVC *jump = [[PaySuccessJumpVC alloc]init];
        
        jump.order_id = self.out_trade_no;
        
        [self.navigationController pushViewController:jump animated:YES];
    }
    

}


- (void)AlipayDidFailNotification{
    
    if ([self.payTag isEqualToString:@"0"]) {
        
        self.payTag = @"1";
        
        PayFaildJumpVC *jump = [[PayFaildJumpVC alloc]init];
        
        jump.order_id = self.out_trade_no;
        [self.navigationController pushViewController:jump animated:YES];
    }

    
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (section == 0) {
        
        
        
        return 4;
    } else {
        return 3;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.section == 0) {
        
        if ([self.type isEqualToString:@"电费"]) {
            
            if (indexPath.row == 3) {
                
                DateSelectCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[DateSelectCell identifierStr]];
                
                tableCell.index = indexPath;
                
                tableCell.model = self.infoModel;
                
                return tableCell;
            } else {
                
                GetMoneyTextFieldCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[GetMoneyTextFieldCell identifierStr]];
                
                tableCell.type = self.type;
                
                tableCell.index = indexPath;
                
                tableCell.model = self.infoModel;
                
                return tableCell;
            }
            
         
            

        } else {
            
            if (indexPath.row == 0) {
                
                DateSelectCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[DateSelectCell identifierStr]];
                 tableCell.index = indexPath;
                tableCell.model = self.infoModel;
                
                return tableCell;
                
              
                
            } else {
                
                GetMoneyTextFieldCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[GetMoneyTextFieldCell identifierStr]];
                
                tableCell.type = self.type;
                
                tableCell.index = indexPath;
                
                tableCell.model = self.infoModel;
                
                return tableCell;
              
            }
            

        }
        

        
    } else {
        
        if (indexPath.row == 2) {
            
            BaseTableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
            
            return tableCell;
            
        } else {
            
            KeepCallsPayCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[KeepCallsPayCell identifierStr]];
            
            tableCell.index = indexPath;
            
            tableCell.model = self.infoModel;

            
            return tableCell;
        }
    }
    
    

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
 
            return 52;
        
        
    } else {
        
        if (indexPath.row == 2) {
            return 0;
        } else {
            return 72;
        }
        
        
    }
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0;
    } else {
        return 44;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        
        return [[UIView alloc]init];
        
    } else {
        
        KeepCallSectionHeader *view = (KeepCallSectionHeader *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:[KeepCallSectionHeader identifierStr]];
        view.rightLeftLabel.hidden = NO;
        
        view.middelLabel.hidden = NO;
        
        view.rightOneLabel.hidden = NO;
        
        self.priceLabel = view.middelLabel;
        
        return view;
    }
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [self.view endEditing:YES];

    [self calculateRmb];
    if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            
            if ([self.infoModel.alipay isEqualToString:@"1"]) {
                
                return;
                
            } else {
                
                self.infoModel.alipay = @"1";
                self.infoModel.wechatPay = @"0";

            }
            
            
        } else {
            
            if ([self.infoModel.wechatPay isEqualToString:@"1"]) {
                
                return;
                
            } else {
                
                self.infoModel.wechatPay = @"1";
                self.infoModel.alipay = @"0";
                
            }
            
            
        }
        
        [self.tableView reloadData];
        
    }else{
        
        if ([self.type isEqualToString:@"电费"]) {
            if (indexPath.row == 3) {
                [self selectDate];
            }
        }else{
            
            
            if (indexPath.row == 0) {
                [self selectType];
            }
        }
        
        
    }
    
}






- (void)selectType{
    
    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
    
    view.isSingle = YES;
    
    NSMutableArray *tmepM = [NSMutableArray array];
    for (int i = 0; i < myViewModel.parameterModel.networkSupplier.count; i++) {
        HouseParameSub *sub = myViewModel.parameterModel.networkSupplier[i];
        [tmepM addObject:[[SelectedListModel alloc] initWithSid:i Title:sub.supplier_name]];
    }

    
    
    view.array = tmepM;
    
    MJWeakSelf;
    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
        
        [LEEAlert closeWithCompletionBlock:^{
            
            weakSelf.infoModel.dateStr = array.firstObject.title;
            
            for (HouseParameSub *sub in myViewModel.parameterModel.networkSupplier) {
                if ([weakSelf.infoModel.dateStr isEqualToString:sub.supplier_name]) {
                    weakSelf.infoModel.supplier_id = sub.supplier_id;
                }
            }
            
            if ([weakSelf.infoModel.dateStr isEqualToString:@"PLDT"]) {
                
                if (myViewModel.user.huHao.length > 0) {
                    
                    if (weakSelf.infoModel.huNumber == nil) {
                        weakSelf.infoModel.huNumber = myViewModel.user.huHao;
                    }
                    
                }
                
                
                if (myViewModel.user.billNumber.length > 0) {
                    
                    if (weakSelf.infoModel.billNumber == nil) {
                        weakSelf.infoModel.billNumber = myViewModel.user.billNumber;
                    }
                    
                }
                
            }

      
            
            [weakSelf.tableView reloadData];
            
        
            
        
            
        }];
        
    };
    
    [LEEAlert alert].config
    .LeeTitle(@"选择供应商")
    .LeeItemInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeCustomView(view)
    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeClickBackgroundClose(YES)
    .LeeShow();
    
    
    
}




- (void)selectDate{
    
    
    __block NSString *province = nil;
    
    __block NSString *city = nil;
    
    __block NSString *area = nil;
    
    
    MJWeakSelf;
    void (^resultBlock)(void) = ^{
        

        
        
        weakSelf.infoModel.dateStr = [NSString stringWithFormat:@"%@%@%@",province,city,area];
        
        [weakSelf.tableView reloadData];
        
        [weakSelf performSelector:@selector(panDuanDate) withObject:nil afterDelay:0.5];
        
        
 
    };
    
    
    NSArray *provinceArray = self.dateArray;
    
    if (provinceArray.count) {
        
        // 获取省数据 并显示
        
        NSMutableArray *array = [NSMutableArray array];
        
        [provinceArray enumerateObjectsUsingBlock:^(NSDictionary *info, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:info[@"name"] Context:info]];
        }];
        
        SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
        
        view.isSingle = YES;
        
        view.array = array;
        
        view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
            
            [LEEAlert closeWithCompletionBlock:^{
                
                // 根据选中的省获取市数据 并显示
                
                SelectedListModel *model = array.firstObject;
                
                province = model.title; //设置省
                
                NSArray *cityArray = model.context[@"sub"];
                
                if (cityArray.count) {
                    
                    NSMutableArray *array = [NSMutableArray array];
                    
                    [cityArray enumerateObjectsUsingBlock:^(NSDictionary *info, NSUInteger idx, BOOL * _Nonnull stop) {
                        
                        [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:info[@"name"] Context:info]];
                    }];
                    
                    SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
                    
                    view.isSingle = YES;
                    
                    view.array = array;
                    
                    view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
                        
                        [LEEAlert closeWithCompletionBlock:^{
                            
                            // 根据选中的市获取区数据 并显示
                            
                            SelectedListModel *model = array.firstObject;
                            
                            city = model.title; //设置市
                            
                            NSArray *areaArray = model.context[@"sub"];
                            
                            if (areaArray.count) {
                                
                                NSMutableArray *array = [NSMutableArray array];
                                
                                [areaArray enumerateObjectsUsingBlock:^(NSString *area, NSUInteger idx, BOOL * _Nonnull stop) {
                                    
                                    [array addObject:[[SelectedListModel alloc] initWithSid:idx Title:area]];
                                }];
                                
                                SelectedListView *view = [[SelectedListView alloc] initWithFrame:CGRectMake(0, 0, 280, 0) style:UITableViewStylePlain];
                                
                                view.isSingle = YES;
                                
                                view.array = array;
                                
                                view.selectedBlock = ^(NSArray<SelectedListModel *> *array) {
                                    
                                    [LEEAlert closeWithCompletionBlock:^{
                                        
                                        // 获取区数据 并显示最终结果
                                        
                                        SelectedListModel *model = array.firstObject;
                                        
                                        area = model.title; //设置区
                                        
                                        //                                                NSLog(@"%@----",area);
                                        
                                        
                                        if (resultBlock) resultBlock();
                                    }];
                                    
                                };
                                
                                [LEEAlert alert].config
                                .LeeTitle(model.title)
                                .LeeCustomView(view)
                                .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
                                .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
                                .LeeClickBackgroundClose(YES)
                                .LeeShow();
                                
                            } else {
                                
                                if (resultBlock) resultBlock();
                            }
                            
                        }];
                        
                    };
                    
                    [LEEAlert alert].config
                    .LeeTitle(model.title)
                    .LeeCustomView(view)
                    .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
                    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
                    .LeeClickBackgroundClose(YES)
                    .LeeShow();
                    
                } else {
                    
                    if (resultBlock) resultBlock();
                }
                
            }];
            
        };
        
        [LEEAlert alert].config
        .LeeTitle(@"选择日期")
        .LeeCustomView(view)
        .LeeItemInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
        .LeeClickBackgroundClose(YES)
        .LeeShow();
    }
    
}

    
    

- (BOOL)panDuanDate{
    
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMdd";

    NSString *nowString = [fmt stringFromDate:[NSDate date]];
    
    NSString *selectStr = [self.infoModel.dateStr stringByReplacingOccurrencesOfString:@"年" withString:@""];
    
    selectStr = [selectStr stringByReplacingOccurrencesOfString:@"月" withString:@""];
    selectStr = [selectStr stringByReplacingOccurrencesOfString:@"日" withString:@""];

    
    if (nowString.integerValue > selectStr.integerValue) {
        
        
        [self showInformation:@"已过截止日,请联系客服"];
        
        return YES;
        
    } else {
        
        return NO;
    }

    
}




- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self.view endEditing:YES];
    [self calculateRmb];
    
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
